package com.sparrow.socket.tcp.session.impl;

import io.netty.util.*;

import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 游戏会话
 */
public class GameSession extends AbstractSession {

    public static int INIT = 0;
    public static int AUTHING = 1;
    public static int AUTHED = 2;

    public static final AttributeKey<GameSession> SESSION_KEY = AttributeKey.valueOf("GameSession");;

    private AtomicInteger status = new AtomicInteger();

    public AtomicInteger getStatus() {
        return status;
    }

    public String getIp() {
        if (channel == null || !channel.isActive()) {
            return "unknown";
        }
        SocketAddress remoteAddress = channel.remoteAddress();
        if (remoteAddress instanceof InetSocketAddress) {
            InetSocketAddress inetSocketAddress = (InetSocketAddress) remoteAddress;
            return inetSocketAddress.getAddress().getHostAddress();
        }
        return remoteAddress.toString(); // fallback
    }

    public String getId() {
        return channel.id().toString();
    }

}
